﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Spremni_List;
using System.Text.RegularExpressions;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification;
using System.Windows.Controls.Primitives;

namespace Spremni_List
{
    class poizvedbe
    {

        MainWindow obrazec;
        ScriptRuntime ironPythonRuntime = Python.CreateRuntime();
        string path = Environment.CurrentDirectory + "\\poizvedbe\\test.py";
        public dynamic loadIPython;


        public poizvedbe(MainWindow _obrazec)
        {
            try
            {
                obrazec = _obrazec;
                ironPythonRuntime.IO.RedirectToConsole();               
                loadIPython = ironPythonRuntime.UseFile(path);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


        public void fireCatch(string pattern, string value)
        {
            try
            {
                string procesna = loadIPython.Catch(pattern, value);
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        public void getPattern(ArrayList list)
        {
            try
            {
                foreach (string d in loadIPython.Patterns(list))
                {
                    obrazec.PatternsReg.Add(new Regex(d));
                }
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        public ArrayList getIzdelki()
        {
            try
            {
                loadIPython.SetVariable("neki", 111111111111111);
                return loadIPython.izdelki();
            }
            catch (Exception e)
            {
                ExceptionOperations eo;

                eo = ironPythonRuntime.GetEngine("py").GetService<ExceptionOperations>();
                string error = eo.FormatException(e);

                string caption;
                string msg = "Syntax error in \"{0}\"";
                caption = String.Format(msg, Path.GetFileName(path));
                Console.WriteLine(caption);
                Console.WriteLine(error);
            }
            return null;
        }

        public ArrayList getDN()
        {
            try
            {
                return loadIPython.delovninalogi(obrazec.comboBox_izdelek.SelectedItem.ToString());
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return null;
        }


        public ArrayList getOperacije()
        {
            try
            {
                return loadIPython.operacije();
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return null;
        }

        public ArrayList getOperater()
        {
            try
            {
                return loadIPython.operaterji();
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return null;
        }

        public string getWbs()
        {
            try
            {
                if (obrazec.comboBox_DN.SelectedItem != null)
                    return loadIPython.wbs(obrazec.comboBox_DN.SelectedItem.ToString());
                else
                    return string.Empty;
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return string.Empty;
        }

        public ArrayList getAllFromProcesna()
        {
            try
            {
                return loadIPython.procesna(obrazec.textBox_procesna_input.Text);

            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            return null;
        }
    }
}
